package pers.jc.netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

public class WebSocketInitializer extends ChannelInitializer<SocketChannel> {
    private String socketURI;

    public WebSocketInitializer(String socketURI) {
        this.socketURI = socketURI;
    }

    @Override
    public void initChannel(SocketChannel ch) {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new HttpServerCodec());
        pipeline.addLast(new HttpObjectAggregator(64 * 1024));
        pipeline.addLast(new ChunkedWriteHandler());
        pipeline.addLast(new HttpRequestHandler(socketURI));
        pipeline.addLast(new WebSocketServerProtocolHandler(socketURI));
        pipeline.addLast(new WebSocketHandler());
//        //新增HTTP功能处理 2020-10-17
//        pipeline.addLast("http-decoder", new HttpRequestDecoder());
//        pipeline.addLast("http-encoder", new HttpResponseEncoder());

//        pipeline.addLast("http-decoder", new HttpRequestDecoder());
//        // 将HTTP消息的多个部分合成一条完整的HTTP消息
//        pipeline.addLast("http-aggregator", new HttpObjectAggregator(65535));
//        // 响应转码器
//        pipeline.addLast("http-encoder", new HttpResponseEncoder());
//        // 解决大码流的问题，ChunkedWriteHandler：向客户端发送HTML5文件
//        pipeline.addLast("http-chunked", new ChunkedWriteHandler());
//        // 自定义处理handler
//        pipeline.addLast("http-server", new HttpRequestHandler(path));
    }
}